package main;

import bean.Express;
import dao.ExpressDao;
import view.View;

import java.util.HashMap;

public class Courier {
    static View view = new View();
    public static void courier(){
        int i = view.courierMenu();
        switch (i){
            case 1:
                addOperate();
                break;
            case 2:
                updateOperate();
                break;
            case 3:
                deleteOperate();
                break;
            case 4:
                HashMap<String, Express> print = ExpressDao.print();
                view.printAll(print);
                break;
            case 0:
                return;
        }
    }
    public static void addOperate(){
        String number = view.addNumber();
        Express express = view.addElse();
        boolean add = ExpressDao.add(number, express);
        if(add){
            view.success(1);
        }else {
            view.fail(1);
        }

    }
    public static void updateOperate(){
        Express express = view.update();
        if(ExpressDao.findByNumber(express.getNumber())){
            boolean update = ExpressDao.update(express.getNumber(), express);
            if(update){
                view.success(2);
            }
        }else{
            view.fail(2);
        }
    }
    public static void deleteOperate(){
        String number = view.findNumber();
        int delete = view.delete();
        if(delete == 1){
            boolean byNumber = ExpressDao.findByNumber(number);
            if(byNumber){
                ExpressDao.delete(number);
                view.success(3);
            }else{
                view.fail(2);
            }
        }else if(delete == 2){
            view.fail(3);
        }
    }
}
